package org.example.jdbc;

import java.lang.module.Configuration;
import java.sql.*;
import java.util.Scanner;

public class homeworkReg {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection()){
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入用户名：");
            String username = scanner.nextLine();
            System.out.println("请输入密码：");
            String password = scanner.nextLine();
            System.out.println("请输入昵称：");
            String nickname = scanner.nextLine();
            System.out.println("请输入年龄：");
            int age = scanner.nextInt();

            PreparedStatement statement = connection.prepareStatement("insert into users values(?,?,?,?)");
            statement.setString(1, username);
            statement.setString(2, password);
            statement.setString(3, nickname);
            statement.setInt(4, age);
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                System.out.println("该用户名已存在，换一个吧~");
            }else{
                PreparedStatement ps = connection.prepareStatement("insert into users values(?,?,?,?)");
                ps.setString(1, username);
                ps.setString(2, password);
                ps.setString(3, nickname);
                ps.setInt(4, age);
                ResultSet resultSet1 = ps.executeQuery();
                System.out.println("注册成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
